package com.yourname.custom;

import com.yourname.entity.Course;
import com.yourname.entity.Teacher;

import java.util.ArrayList;

public class CustomTeacherList extends ArrayList<Teacher> {
    @Override
    public boolean contains(Object o) {
        Teacher s=(Teacher) o;
        for (Teacher Teacher:this){
            if (Teacher.getId().equals(s.getId())){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean add(Teacher teacher) {
        if (contains(teacher)){
            System.out.printf("教师%s已存在",teacher.getId());
            return false;
        }else{
            return super.add(teacher);
        }
    }
    public Teacher get(String sid) {
        for (Teacher Teacher:this){
            if (Teacher.getId().equals(sid)){
                return Teacher;
            }
        }
        return null;
    }
    public boolean update(String sid,Teacher newTeacher) {
        for (int i=0;i<this.size();i++){
            if (this.get(i).getId().equals(sid)){
                this.set(i,newTeacher);
                return true;
            }
        }
        return false;
    }

    public Teacher remove(String sid) {
        for (int i=0;i<this.size();i++){
            if (this.get(i).getId().equals(sid)){
                return this.remove(i);
            }
        }
        return null;
    }
    public Teacher set(Teacher newCourse){
        Teacher ori=null;
        for (int i=0;i<this.size();i++){
            Teacher target=this.get(i);
            ori=target;
            if (this.get(i).getId().equals(newCourse.getId())){
                return this.set(i,newCourse);
            }
        }
        return ori;
    }
}
